package com.jungle.client.control;

import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;

public class Form<T> extends Parent {
    /**
     * 加载数据
     *
     * @param data 数据
     */
    public void loadData(T data) {
        Node lookup = this.lookup("#attr1");
        if (lookup instanceof Control control) {

        }
    }

    /**
     * 重置数据
     */
    public void resetData() {

        this.getChildren().forEach(node -> {
            if (node instanceof TextField textField) {
                //String
                textField.setText(null);
            } else if (node instanceof PasswordField passwordField) {
                //String
                passwordField.setText(null);
            } else if (node instanceof TextArea textArea) {
                //String
                textArea.setText(null);
            } else if (node instanceof DatePicker datePicker) {
                //LocalDate
                datePicker.setValue(null);
            } else if (node instanceof Slider slider) {
                //double
                slider.setValue(0);
            } else if (node instanceof ChoiceBox choiceBox) {
                choiceBox.setValue(null);
            } else if (node instanceof ComboBox comboBox) {
                // Object(String)
                comboBox.setValue(null);
            } else if (node instanceof ColorPicker colorPicker) {
                // Color
                colorPicker.setValue(null);
            } else if (node instanceof CheckBox checkBox) {
                // boolean
                checkBox.setSelected(false);
            } else if (node instanceof ToggleButton toggleButton) {
                // boolean
                toggleButton.setSelected(false);
            } else if (node instanceof RadioButton radioButton) {
                // boolean
                radioButton.setSelected(false);
            } else if (node instanceof Spinner spinner) {
                //int\double\LocalDate\LocalTime
                spinner.getValueFactory().setValue(null);
            }
        });
    }

    /**
     * 获取表单数据
     *
     * @return 数据
     */
    public T fetchData() {
        return null;
    }

    /**
     * 强化、完善获取的表单数据
     *
     * @param data 表单数据
     */
    protected T enhanceData(T data) {
        return data;
    }

}
